package com.syqc.monitor.ui.cost

import android.content.Context
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.setResultTo
import com.syqc.entity.DriverInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivitySearchRecyclerBinding
import com.syqc.utils.Keys

/**
 *@Author Jasper
 *@Time   2020/12/16
 *@Desc   驾驶员列表
 */
class DriversActivity :
    BaseRecycleActivity<ActivitySearchRecyclerBinding>(ActivitySearchRecyclerBinding::inflate) {
    private val mAdapter by lazy { DriversAdapter(this) }
    private var loadService: LoadService<Any>? = null
    private val mViewModel by viewModels<DriversViewModel>()


    override fun initView() {
        super.initView()
        val isDriver = intent.getBooleanExtra("isDriver", true)
        ToolbarHelper.setToolbar(
            this,
            if (isDriver) R.string.title_report_drivers else R.string.desc_add_oil_prople
        )
        val hint = if (isDriver) {
            R.string.hint_search_toast_driver
        } else {
            R.string.desc_input_oil_people
        }
        binding.editSearch.setHint(hint)
        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.adapter = mAdapter
        loadService = LoadSir.getDefault().register(mRecyclerVB.recyclerView)

        loadService?.showCallback(EmptyCallback::class.java)

        mViewModel.getDriversData()
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        mViewModel.dataObserver().autoDispose(scopeProvider).subscribe {
            if (it.isNullOrEmpty()) {
                loadService?.showCallback(EmptyCallback::class.java)
            } else {
                loadService?.showSuccess()
            }
            mAdapter.setList(it)
        }
    }

    override fun setListener() {
        binding.tvSearch.setOnClickListener {
            mViewModel.searchDrivers(binding.editSearch.text.toString())
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            setResultTo(this) {
                putExtra(Keys.INTENT_ID, mAdapter.data[position].id)
                putExtra(Keys.INTENT_OTHER, mAdapter.data[position].name)
            }
            finish()
        }
    }

    inner class DriversAdapter(mContext: Context) :
        BaseQuickAdapter<DriverInfo, BaseViewHolder>(R.layout.item_type_report) {
        private val sex_man = mContext.getString(R.string.desc_sex_man)
        private val sex_woman = mContext.getString(R.string.desc_sex_woman)
        override fun convert(holder: BaseViewHolder, item: DriverInfo) {
            item.run {
                holder.setText(
                    R.id.tv_type_title,
                    "$name (${if (sex == "1") sex_man else sex_woman})"
                )
            }
        }
    }
}